<template>
  <div class="customers">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>客户管理</span>
          <el-button type="primary" @click="handleAdd">添加客户</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="客户昵称">
          <el-input
            v-model="searchForm.nickname"
            placeholder="请输入客户昵称"
          />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="searchForm.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="微信号">
          <el-input v-model="searchForm.wechat" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="客户类型">
          <el-select v-model="searchForm.type" placeholder="请选择客户类型">
            <el-option label="二手车" value="used" />
            <el-option label="租车" value="rent" />
            <el-option label="新车" value="new" />
          </el-select>
        </el-form-item>
        <el-form-item label="客户征信">
          <el-select v-model="searchForm.credit" placeholder="请选择征信">
            <el-option label="白" value="white" />
            <el-option label="黑" value="black" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="customerList" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="name" label="客户昵称" width="120" />
        <el-table-column prop="phone" label="电话" width="120" />
        <el-table-column prop="wechat" label="微信号" width="120" />
        <el-table-column prop="type" label="客户类型" width="100">
          <template #default="{ row }">
            <el-tag :type="getTypeTag(row.type)">
              {{ row.type }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="credit" label="征信" width="80">
          <template #default="{ row }">
            <el-tag :type="row.credit === '白' ? 'success' : 'danger'">
              {{ row.credit }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="carType" label="意向车型" width="120" />
        <el-table-column prop="sales" label="销售人员" width="120" />
        <el-table-column prop="group" label="小组" width="100" />
        <el-table-column prop="leader" label="组长" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button type="primary" link @click="handleViewDetails(row)"
              >详情</el-button
            >
            <el-button type="primary" link @click="handleViewFlow(row)"
              >跟进记录</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 客户详情对话框 -->
    <el-dialog title="客户详情" v-model="detailsDialogVisible" width="800px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="客户昵称">{{
          currentCustomer.nickname
        }}</el-descriptions-item>
        <el-descriptions-item label="电话">{{
          currentCustomer.phone
        }}</el-descriptions-item>
        <el-descriptions-item label="微信号">{{
          currentCustomer.wechat
        }}</el-descriptions-item>
        <el-descriptions-item label="客户类型">{{
          currentCustomer.type
        }}</el-descriptions-item>
        <el-descriptions-item label="征信">{{
          currentCustomer.credit
        }}</el-descriptions-item>
        <el-descriptions-item label="意向车型">{{
          currentCustomer.carType
        }}</el-descriptions-item>
        <el-descriptions-item label="销售人员">{{
          currentCustomer.sales
        }}</el-descriptions-item>
        <el-descriptions-item label="小组">{{
          currentCustomer.group
        }}</el-descriptions-item>
        <el-descriptions-item label="组长">{{
          currentCustomer.leader
        }}</el-descriptions-item>
        <el-descriptions-item label="获客渠道">{{
          currentCustomer.channel
        }}</el-descriptions-item>
        <el-descriptions-item label="居住地">{{
          currentCustomer.location
        }}</el-descriptions-item>
        <el-descriptions-item label="户籍地">{{
          currentCustomer.hometown
        }}</el-descriptions-item>
        <el-descriptions-item label="预约时间">{{
          currentCustomer.appointmentTime
        }}</el-descriptions-item>
        <el-descriptions-item label="到店时间">{{
          currentCustomer.visitTime
        }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 跟进记录对话框 -->
    <el-dialog title="跟进记录" v-model="flowDialogVisible" width="800px">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
          :type="activity.type"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>

    <!-- 添加/编辑客户对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入客户昵称" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="微信" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入微信" />
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-select v-model="form.source" placeholder="请选择来源">
            <el-option label="抖音" value="抖音" />
            <el-option label="视频号" value="视频号" />
            <el-option label="快手" value="快手" />
            <el-option label="直播" value="直播" />
            <el-option label="小红书" value="小红书" />
          </el-select>
        </el-form-item>
        <el-form-item label="咨询类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择咨询类型">
            <el-option label="租车" value="租车" />
            <el-option label="买车" value="买车" />
            <el-option label="二手车" value="二手车" />
          </el-select>
        </el-form-item>
        <el-form-item label="考虑车型" prop="carType">
          <el-input v-model="form.carType" placeholder="请输入考虑车型" />
        </el-form-item>
        <el-form-item label="跟进销售" prop="sales">
          <el-select v-model="form.sales" placeholder="请选择销售">
            <el-option label="张三" value="张三" />
            <el-option label="李四" value="李四" />
            <el-option label="王五" value="王五" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="待跟进" value="待跟进" />
            <el-option label="已联系" value="已联系" />
            <el-option label="已到店" value="已到店" />
            <el-option label="已成交" value="已成交" />
            <el-option label="已流失" value="已流失" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getCustomList } from "@/api/customer";

const searchForm = reactive({
  nickname: "",
  phone: "",
  wechat: "",
  type: "",
  credit: "",
});
const customerList = ref([]);
function getCustomerList() {
  getCustomList().then((res) => {
    customerList.value = res;
  });
}
onMounted(() => {
  getCustomerList();
});

const customersList = ref([
  {
    id: 1,
    nickname: "客户A",
    phone: "138****1234",
    wechat: "wx123456",
    type: "二手车",
    credit: "白",
    carType: "面包车",
    sales: "张三",
    group: "A组",
    leader: "李四",
    status: "跟进中",
    channel: "短视频",
    location: "北京市朝阳区",
    hometown: "河北省石家庄市",
    appointmentTime: "2024-03-21 14:00",
    visitTime: "2024-03-21 14:30",
  },
  {
    id: 2,
    nickname: "客户B",
    phone: "139****5678",
    wechat: "wx234567",
    type: "租车",
    credit: "黑",
    carType: "箱货",
    sales: "王五",
    group: "B组",
    leader: "赵六",
    status: "已成交",
    channel: "地推",
    location: "上海市浦东新区",
    hometown: "江苏省南京市",
    appointmentTime: "2024-03-20 10:00",
    visitTime: "2024-03-20 10:30",
  },
]);

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(100);
const detailsDialogVisible = ref(false);
const flowDialogVisible = ref(false);
const currentCustomer = ref({});
const dialogVisible = ref(false);
const dialogTitle = ref("添加客户");
const formRef = ref(null);

const activities = ref([
  {
    content: "客户咨询",
    timestamp: "2024-03-20 14:30",
    type: "primary",
  },
  {
    content: "电话沟通",
    timestamp: "2024-03-20 15:00",
    type: "success",
  },
  {
    content: "预约到店",
    timestamp: "2024-03-21 10:00",
    type: "warning",
  },
  {
    content: "到店详谈",
    timestamp: "2024-03-21 14:30",
    type: "info",
  },
]);

const form = reactive({
  nickname: "",
  phone: "",
  wechat: "",
  source: "",
  type: "",
  carType: "",
  sales: "",
  status: "待跟进",
});

const rules = {
  nickname: [{ required: true, message: "请输入客户昵称", trigger: "blur" }],
  phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
  wechat: [{ required: true, message: "请输入微信", trigger: "blur" }],
  source: [{ required: true, message: "请选择来源", trigger: "change" }],
  type: [{ required: true, message: "请选择咨询类型", trigger: "change" }],
  carType: [{ required: true, message: "请输入考虑车型", trigger: "blur" }],
  sales: [{ required: true, message: "请选择销售", trigger: "change" }],
  status: [{ required: true, message: "请选择状态", trigger: "change" }],
};

const getTypeTag = (type) => {
  const types = {
    二手车: "warning",
    租车: "success",
    新车: "primary",
  };
  return types[type] || "info";
};

const getStatusType = (status) => {
  const types = {
    跟进中: "warning",
    已到店: "primary",
    已成交: "success",
    已放弃: "danger",
  };
  return types[status] || "info";
};

const handleSearch = () => {
  // 实现搜索逻辑 console.log("搜索条件：", searchForm);
};

const resetSearch = () => {
  Object.keys(searchForm).forEach((key) => {
    searchForm[key] = "";
  });
};

const handleAdd = () => {
  dialogTitle.value = "添加客户";
  dialogVisible.value = true;
  Object.keys(form).forEach((key) => {
    form[key] = key === "status" ? "待跟进" : "";
  });
};

const handleEdit = (row) => {
  dialogTitle.value = "编辑客户";
  dialogVisible.value = true;
  Object.assign(form, row);
};

const handleViewDetails = (row) => {
  currentCustomer.value = row;
  detailsDialogVisible.value = true;
};

const handleViewFlow = (row) => {
  flowDialogVisible.value = true;
};

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 实现提交逻辑
      ElMessage.success("保存成功");
      dialogVisible.value = false;
    }
  });
};

const handleSizeChange = (val) => {
  pageSize.value = val;
  // 实现分页逻辑
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
  // 实现分页逻辑
};
</script>

<style scoped>
.customers {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 